home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Imaging / PIL / GimpGradientFile.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  2.8 KB  |  124 lines

  1. #
  2. # Python Imaging Library
  3. # $Id: GimpGradientFile.py,v 1.1.1.1 1998/08/18 13:07:55 sjoerd Exp $
  4. #
  5. # stuff to read (and render) GIMP gradient files
  6. #
  7. # History:
  8. #    97-08-23 fl    Created
  9. #
  10. # Copyright (c) Secret Labs AB 1997.
  11. # Copyright (c) Fredrik Lundh 1997.
  12. #
  13. # See the README file for information on usage and redistribution.
  14. #
  15.  
  16. from math import pi, log, sin, sqrt
  17. import string, sys
  18.  
  19. # --------------------------------------------------------------------
  20. # Stuff to translate curve segments to palette values (derived from
  21. # the corresponding code in GIMP, written by Federico Mena Quintero.
  22. # See the GIMP distribution for more information.)
  23. #
  24.  
  25. EPSILON = 1e-10
  26.  
  27. def linear(middle, pos):
  28.     if pos <= middle:
  29.     if middle < EPSILON:
  30.         return 0.0
  31.     else:
  32.         return 0.5 * pos / middle
  33.     else:
  34.         pos = pos - middle
  35.     middle = 1.0 - middle
  36.     if middle < EPSILON:
  37.         return 1.0
  38.     else:
  39.         return 0.5 + 0.5 * pos / middle
  40.  
  41. def curved(middle, pos):
  42.     return pos ** (log(0.5) / log(max(middle, EPSILON)))
  43.  
  44. def sine(middle, pos):
  45.     return (sin((-pi / 2.0) + pi * linear(middle, pos)) + 1.0) / 2.0
  46.  
  47. def sphere_increasing(middle, pos):
  48.     return sqrt(1.0 - (linear(middle, pos) - 1.0) ** 2)
  49.  
  50. def sphere_decreasing(middle, pos):
  51.     return 1.0 - sqrt(1.0 - linear(middle, pos) ** 2)
  52.  
  53. SEGMENTS = [ linear, curved, sine, sphere_increasing, sphere_decreasing ]
  54.  
  55. # --------------------------------------------------------------------
  56. #
  57.  
  58. class GradientFile:
  59.  
  60.     def getpalette(self, entries = 256):
  61.  
  62.     palette = []
  63.  
  64.     ix = 0
  65.     x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix]
  66.  
  67.     for i in range(entries):
  68.  
  69.         x = i / float(entries-1)
  70.  
  71.         while x1 < x:
  72.         ix = ix + 1
  73.         x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix]
  74.  
  75.         w = x1 - x0
  76.  
  77.         if w < EPSILON:
  78.         scale = segment(0.5, 0.5)
  79.         else:
  80.         scale = segment((xm - x0) / w, (x - x0) / w)
  81.  
  82.         # expand to RGBA
  83.         r = chr(int(255 * ((rgb1[0] - rgb0[0]) * scale + rgb0[0]) + 0.5))
  84.         g = chr(int(255 * ((rgb1[1] - rgb0[1]) * scale + rgb0[1]) + 0.5))
  85.         b = chr(int(255 * ((rgb1[2] - rgb0[2]) * scale + rgb0[2]) + 0.5))
  86.         a = chr(int(255 * ((rgb1[3] - rgb0[3]) * scale + rgb0[3]) + 0.5))
  87.  
  88.         # add to palette
  89.         palette.append(r + g + b + a)
  90.  
  91.     return string.join(palette, ""), "RGBA"
  92.  
  93.  
  94. class GimpGradientFile(GradientFile):
  95.  
  96.     def __init__(self, fp):
  97.  
  98.     if fp.readline()[:13] != "GIMP Gradient":
  99.         raise SyntaxError, "not a GIMP gradient file"
  100.  
  101.     count = string.atoi(fp.readline())
  102.  
  103.     gradient = []
  104.  
  105.     for i in range(count):
  106.  
  107.         s = string.split(fp.readline())
  108.         w = map(string.atof, s[:11])
  109.  
  110.         x0, x1  = w[0], w[2]
  111.         xm      = w[1]
  112.         rgb0    = w[3:7]
  113.         rgb1    = w[7:11]
  114.  
  115.         segment = SEGMENTS[string.atoi(s[11])]
  116.         cspace  = string.atoi(s[12])
  117.  
  118.         if cspace != 0:
  119.         raise IOError, "cannot handle HSV colour space"
  120.  
  121.         gradient.append((x0, x1, xm, rgb0, rgb1, segment))
  122.  
  123.     self.gradient = gradient
  124.